﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using ImportEnt;

namespace WebImport
{
    /// <summary>
    /// Import    导入Xml
    /// </summary>
    public class ImportXml : IHttpHandler
    {
        private string tokinId = "1181250294x";

        public void ProcessRequest(HttpContext context)
        {

            StreamReader reader = new StreamReader(context.Request.InputStream, Encoding.UTF8);

            string strJson = reader.ReadToEnd();
            if (string.IsNullOrEmpty(strJson))
            {
                context.Response.Write("JsonError");
                return;
            }

            var ent = Serializer.XMLToObject<DiarysXml>(strJson);


            context.Response.ContentType = "text/plain";
            string tokin = ent.Tokin;
            if (tokin != tokinId)
            {
                context.Response.Write("TokinError");
                return;
            }
            try
            {


                string action = ent.Action;
                string strRes = string.Empty;
                switch (action)
                {
                    case "addDiary":
                        foreach (var ite in ent.MessageList)
                        {


                            string title = ite.DiaryTitle;
                            string content = ite.DiaryContent;
                            string tag = ite.DiaryTag;
                            int typeId = ite.DiaryTypeId;
                            content = HttpUtility.HtmlEncode(content);
                            var diaryService = new DiaryService();

                            Diarys data = new Diarys()
                            {
                                DiaryTitle = HttpUtility.HtmlEncode(title),
                                DiaryContent = content,
                                CreateUser = 100,
                                DiaryTypeId = typeId,
                                DiaryTag = HttpUtility.HtmlEncode(tag),
                                CreateDate = DateTime.Now
                            };

                            //验证数据是否存在
                            bool isTitleExist = diaryService.CheckTitleIsExists(data.DiaryTitle);
                            if (isTitleExist)
                            {
                                context.Response.Write("Title Exist");
                                return;
                            }


                            int dairyId = diaryService.InsertDiary(data);
                            bool isOk = dairyId > 0;

                            if (isOk)
                            {
                                strRes = ("OK");
                            }
                            else
                            {
                                strRes = ("Error");
                            }

                        }
                        context.Response.Write(strRes);
                        break;
                    default:
                        context.Response.Write("无Action参数");
                        break;
                }
            }
            catch (Exception ex)
            {
                context.Response.Write("Error:" + ex);
            }
        }




        public bool IsReusable
        {
            get { return false; }
        }
    }


}